package web.audio;

import java.util.Arrays;
import java.util.List;
import java.util.Vector;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.ListChoice;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.wicketstuff.html5.media.MediaSource;
import org.wicketstuff.html5.media.audio.Html5Audio;

public class AudioHome extends WebPage {
	private static final long serialVersionUID = 1L;
	private List<String> temas;
	private String seleccionado;
	private List<MediaSource> listaDeReproduccion;
	private Component form;

	public AudioHome() {
		inicializar();
		this.add(
			form = new Form<Void>("form").add(
				temas(),
				tema_agregar(),
				listaDeReproduccion(),
				reproduccionActual()
			),
			reproductor()
		);
		form.setOutputMarkupId(true);
	}


	private void inicializar() {
		this.temas = Arrays.asList("219-game-over.mp3", "Life And Death.mp3", "Win One For The Reaper.mp3");
		this.seleccionado = this.temas.get(0);
		this.listaDeReproduccion = new Vector<MediaSource>(
										Arrays.asList(
//											new MediaSource("files/219-game-over.mp3"),
											new MediaSource("files/Life_And_Death.mp3")
//											new MediaSource("files/Win_One_For_The_Reaper.mp3")
										)
									);
	}


	private Component reproductor() {
		IModel<List<MediaSource>> source = new PropertyModel<List<MediaSource>>(this, "listaDeReproduccion"); 
		Html5Audio reproductor = new ReproductorDeAudio("reproductor", source);
		return reproductor;
	}

	private Component listaDeReproduccion() {
		IModel<List<MediaSource>> source = new PropertyModel<List<MediaSource>>(this, "listaDeReproduccion");
		return new ListView<MediaSource>("listaDeReproduccion", source) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<MediaSource> item) {
				final MediaSource media = item.getModelObject();
				item.add(new Label("tema_nombre", media.getSrc()));
			}
		};
	}

	private Component tema_agregar() {
		return new AjaxButton("tema_agregar") {
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				listaDeReproduccion.add(new MediaSource("files/"+seleccionado));
				target.add(form);
			}
			
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {}
		};
	}
	
	private Component reproduccionActual() {
		IModel<String> modelo = new AbstractReadOnlyModel<String>() {
			private static final long serialVersionUID = 1L;

			@Override
			public String getObject() {
				return listaDeReproduccion.get(0).getSrc();
			}
		};
		return new Label("reproduccionActual", modelo);
	}

	private Component temas() {
		IModel<String> modelo = new PropertyModel<String>(this, "seleccionado");
		return new ListChoice<String>("temas", modelo, temas);
	}
}